package cn.fl.carloan.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fl.preloan.condition.AuditCarInfoQueryCondition;
import cn.fl.preloan.condition.AuditCreditInfoQueryCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.audit.condition.AudCarInfoQueryCondition;
import cn.fl.audit.condition.AudCreditInfoQueryCondition;
import cn.fl.audit.service.IAudCarInfoService;
import cn.fl.audit.service.IAudCreditInfoService;
import cn.fl.audit.vo.AudCarInfoVO;
import cn.fl.audit.vo.AudCreditInfoVO;
import cn.fl.carloan.service.IOdrPrdBscInfoService;
import cn.fl.carloan.transform.OdrBscInfoVOTransform;
import cn.fl.carloan.vo.OdrBscInfoVO;
import cn.fl.carloan.vo.OdrPrdBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjPrdDtlService;
import cn.fl.system.constant.SysLogConstant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import fl.carloan.facade.carloan.api.IOdrBscInfoApiService;
import fl.carloan.facade.carloan.api.IOdrPrdBscInfoApiService;
import fl.carloan.facade.carloan.condition.OdrPrdBscInfoQC;

/**
 * @Description:  控制层
 */
@Controller
@RequestMapping("/carloan/odrprdbscinfo")
public class OdrPrdBscInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(OdrPrdBscInfoController.class);

	/**
	 * OdrPrdBscInfo 服务层BEAN
	 */
	@Autowired
	private IOdrPrdBscInfoService odrPrdBscInfoService;

	@Autowired
	private IPrjPrdDtlService prjPrdDtlService;

	@Autowired
	private IOdrPrdBscInfoApiService odrPrdBscInfoApiService;

	@Autowired
	private ISysRegionApiService sysRegionApiService;
	
	@Autowired
	private IOdrBscInfoApiService odrBscInfoApiService;
	
	@Autowired
	private IAudCreditInfoService audCreditInfoService;
	
	@Autowired
	private IAudCarInfoService audCarInfoService;
	
	/**
	 * 信审修改信审最高可贷金额
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/modifyOdrPrd.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyOdrPrd(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_OPERATE_SUCC);
		String odrPrdId = request.getParameter("odrPrdId");
		String auditPrdAmt = request.getParameter("auditPrdAmt");
		String lnkCompId = request.getParameter("lnkCompId");
		String lnkCompNm = request.getParameter("lnkCompNm");
		String odrId = request.getParameter("odrId");
		String isOwnCensus = request.getParameter("isOwnCensus");
		if(StringUtils.isEmpty(odrPrdId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "预约产品ID不能为空.");
		}
		if(StringUtils.isEmpty(odrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "预约ID不能为空.");
		}
		if(StringUtils.isEmpty(auditPrdAmt)&&StringUtils.isEmpty(lnkCompId)&&StringUtils.isEmpty(lnkCompNm)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "没有要保存的信息.");
		}
		OdrPrdBscInfoVO entity = new OdrPrdBscInfoVO();
		try {
			entity.setId(Long.valueOf(odrPrdId));
			entity.setAuditPrdAmt(StringUtils.isNotEmpty(auditPrdAmt)?DecimalUtils.convertYuanToFen(Double.valueOf(auditPrdAmt)):null);
			if(StringUtils.isNotEmpty(lnkCompId)) entity.setLnkCompId(Long.valueOf(lnkCompId));
			entity.setLnkCompNm(lnkCompNm);
			boolean success = this.odrPrdBscInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
				BaseLogUtils.info(logger, "modifyOdrPrd", "信审修改信审最高可贷金额失败.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
				return result;
			}
			if(StringUtils.isNotEmpty(isOwnCensus)){
				OdrBscInfoVO odrBscInfoVO = new OdrBscInfoVO();
				odrBscInfoVO.setId(Long.valueOf(odrId));
				odrBscInfoVO.setIsOwnCensus(Integer.valueOf(isOwnCensus));
				boolean modifyOdrBscInfoDTO = this.odrBscInfoApiService.modifyOdrBscInfoDTO(OdrBscInfoVOTransform.toDTO(odrBscInfoVO));
				if(!modifyOdrBscInfoDTO){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
					BaseLogUtils.info(logger, "modifyOdrPrd", "信审修改是否个人户失败.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
					return result;
				}
			}
			BaseLogUtils.info(logger, "modifyOdrPrd", "信审修改信审最高可贷金额成功.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.info(logger, "modifyOdrPrd", "信审修改信审最高可贷金额失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.error(logger, "modifyOdrPrd", "信审修改信审最高可贷金额失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	@RequestMapping(value = "/initOdrPrdList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initOdrPrdList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String prjId = request.getParameter("prjId");//项目ID=预约ID
		String proIntId = request.getParameter("proIntId");//流程实例ID
		try {
			if(StringUtils.isEmpty(prjId)){
				BaseLogUtils.info(logger, "initOdrPrdList", "初始化项目产品列表,项目ID不能为空.projectId:"+prjId, CurrentThreadContext.getCurrentUserName());
				return null;
			}
			Pager page = new Pager();
			if (StringUtils.isNotEmpty(currentPage)) {
				page.setCurrentPage(Integer.parseInt(currentPage));
			}
			if (StringUtils.isNotEmpty(currentPage)) {
				page.setEveryPage(Integer.parseInt(pageSize));
			}

			OdrPrdBscInfoQC qc = new OdrPrdBscInfoQC();
			if(StringUtils.isNotBlank(prjId)){
				qc.setOdrId(Long.valueOf(prjId));
			}
			page.setCondition(qc);

			BaseLogUtils.info(logger, "initOdrPrdList", "=查询项目产品信息Start=page:"+JSON.toJSONString(page), CurrentThreadContext.getCurrentUserName());
			Pager pager = this.odrPrdBscInfoService.searchListPage(page);
			BaseLogUtils.info(logger, "initOdrPrdList", "=查询项目产品信息End=pager:"+JSON.toJSONString(pager), CurrentThreadContext.getCurrentUserName());
			@SuppressWarnings("unchecked")
			List<OdrPrdBscInfoVO> list = (List<OdrPrdBscInfoVO>) pager.getResultList();
			
			//加入信审维护产品信息
			if(CollectionUtils.isNotEmpty(list)){
				//为防止页面产品列表报audCarInfoVO null 异常 先都设为空对象
				for (OdrPrdBscInfoVO odrPrdBscInfoVO : list) {
					odrPrdBscInfoVO.setAudCarInfoVO(new AudCarInfoVO());
				}
				//查询信审资料表
				if(StringUtils.isNotEmpty(proIntId)){
					AuditCreditInfoQueryCondition creditQC = new AuditCreditInfoQueryCondition();
					creditQC.setProIntId(proIntId);
					creditQC.setProjectId(Long.valueOf(prjId));
					AudCreditInfoVO audCreditInfoVO = this.audCreditInfoService.selectAudCreditInfoByQc(creditQC);
					Long creditInfoId = audCreditInfoVO.getId();
					if(null!=creditInfoId){
						List<Long> prjPrdIdList = new ArrayList<Long>();
						for (OdrPrdBscInfoVO OdrPrdBscInfoVO : list) {
							prjPrdIdList.add(OdrPrdBscInfoVO.getId());
						}
						AuditCarInfoQueryCondition condition = new AuditCarInfoQueryCondition();
						condition.setPrjPrdIdList(prjPrdIdList);
						condition.setCreditInfoId(creditInfoId);
						List<AudCarInfoVO> audCarInfoVOList = this.audCarInfoService.searchList(condition);
						if(CollectionUtils.isNotEmpty(audCarInfoVOList)){
							for (AudCarInfoVO audCarInfoVO : audCarInfoVOList) {
								for (OdrPrdBscInfoVO odrPrdBscInfoVO : list) {
									if(odrPrdBscInfoVO.getId().equals(audCarInfoVO.getPrjPrdId())){
										odrPrdBscInfoVO.setAudCarInfoVO(audCarInfoVO);
									}
								}
							}
						}
					}
				}
			}
			
			ListPageVO<OdrPrdBscInfoVO> listPage = new ListPageVO<OdrPrdBscInfoVO>(list, pager.getTotalCount());
			BaseLogUtils.info(logger, "initOdrPrdList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
			return listPage;
		}catch (Exception e) {
			BaseLogUtils.info(logger, "initOdrPrdList", "=初始化产品列表数据异常=prjId:"+prjId+","+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}

